// UserNetwork.cs
// Facebook/Framework/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using Facebook.Service.Core;

namespace Facebook.Service {

    /// <summary>
    /// Represents a Facebook network.
    /// </summary>
    public sealed class UserNetwork : FacebookEntity {

        private bool _typeTransformed;
        private bool _idTransformed;

        internal UserNetwork(Hashtable networkState)
            : base(networkState) {
        }

        /// <summary>
        /// The unique ID of the network.
        /// </summary>
        public string ID {
            get {
                if (_idTransformed == false) {
                    return TransformID();
                }
                return GetState<string>("nid");
            }
        }

        /// <summary>
        /// The name of the network.
        /// </summary>
        public string Name {
            get {
                return GetState<string>("name") ?? String.Empty;
            }
        }

        /// <summary>
        /// The status of the network, such as graduation status if the network
        /// is a college network.
        /// </summary>
        public string Status {
            get {
                return GetState<string>("status") ?? String.Empty;
            }
        }

        /// <summary>
        /// The type of the network.
        /// </summary>
        public UserNetworkType Type {
            get {
                if (_typeTransformed == false) {
                    return TransformType();
                }
                else {
                    return (UserNetworkType)this["type"];
                }
            }
        }

        /// <summary>
        /// The year associated with the network, if any.
        /// </summary>
        public string Year {
            get {
                return GetState<string>("year");
            }
        }

        private string TransformID() {
            Debug.Assert(_idTransformed == false);

            string id = FacebookResponse.CoerceString(this["nid"]);

            this["nid"] = id;
            _idTransformed = true;

            return id;
        }

        private UserNetworkType TransformType() {
            Debug.Assert(_typeTransformed == false);

            UserNetworkType type = UserNetworkType.Other;
            string typeName = GetState<string>("type") ?? String.Empty;

            if (typeName.Equals("college")) {
                type = UserNetworkType.College;
            }
            else if (typeName.Equals("work")) {
                type = UserNetworkType.Work;
            }
            else if (typeName.Equals("region")) {
                type = UserNetworkType.Region;
            }
            else if (typeName.Equals("high school")) {
                type = UserNetworkType.HighSchool;
            }

            this["type"] = type;
            _typeTransformed = true;

            return type;
        }
    }
}
